﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Debugger : MonoBehaviour
{
    public static Debugger Instance;

    private bool _isActive = false;
    private float _totalSeconds = 0;
    private int _totalFrameCount = 0;

    private string _displayMessage = "ready";

    private void Awake()
    {
        Instance = this;
    }

    // Update is called once per frame
    private void Update()
    {
        if (_isActive)
        {
            _totalSeconds += Time.deltaTime;
            _totalFrameCount++;
        }
    }

    private void OnGUI()
    {
        GUILayout.Label(_displayMessage);
    }

    public void MarkBegin()
    {
        _isActive = true;
        _totalSeconds = 0;
        _totalFrameCount = 0;

        _displayMessage = "Working ...";
    }

    public void MarkEnd()
    {
        _isActive = false;
        var fps = _totalFrameCount / _totalSeconds;
        _displayMessage = string.Format("Fps {0} in {1} seconds.", fps, _totalSeconds);
    }

    public void CalFpsInSeconds(float seconds)
    {
        MarkBegin();
        StartCoroutine(DelayMarkEnd(seconds));
    }

    private IEnumerator DelayMarkEnd(float seconds)
    {
        yield return new WaitForSeconds(seconds);
        MarkEnd();
    }
}
